package com.tuanzi.loan.business.service.businesssystem.impl;

import com.tuanzi.loan.business.entity.businesssystem.BusinessMain;
import com.tuanzi.loan.business.entity.businesssystem.BusinessPrepay;
import com.tuanzi.loan.business.repository.businesssystem.BusinessMainRepository;
import com.tuanzi.loan.business.repository.businesssystem.BusinessPrepayRepository;
import com.tuanzi.loan.business.service.businesssystem.BusinessPrepayService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.businesssystem.BusinessPrepayRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessPrepayResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author caiwanlin
 * @create 2018-08-22 17:39
 */
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class BusinessPrepayServiceImpl extends BaseAuditServiceImpl<BusinessPrepay, Long> implements BusinessPrepayService {
    @Autowired
    private BusinessPrepayRepository settlementPrepayRepository;

    @Autowired
    private BusinessMainRepository settlementCompanyRepository;

    private ReentrantLock lock = new ReentrantLock();

    @Override
    protected JPAAuditRepository<BusinessPrepay, Long> getRepository() {
        return settlementPrepayRepository;
    }

    @Override
    public Page<BusinessPrepayResponse> list(PageSearcher<BusinessPrepayRequest> pageSearcher) {
        return settlementPrepayRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    @Override
    public void onSave(BusinessPrepay settlementPrepay) {
        Long settlementPrepayId = settlementPrepay.getId();

        //更新公司的预付金额

        BusinessMain company = null;
        BusinessPrepay oldBill =null;
        Double settlementPrepayAmount = null;//充值金额
        Double companyPrepayAmount = null;
        try {
            lock.lock();//保证只有一个线程在执行更新操作
            company = settlementCompanyRepository.findOne(settlementPrepay.getCompanyId());
            companyPrepayAmount = company.getPrepayAmount();
            settlementPrepayAmount = settlementPrepay.getPrepayAmount();
            if (companyPrepayAmount == null){
                company.setPrepayAmount(settlementPrepayAmount);
            }
            else {
                if (settlementPrepayId == null){ //如果是添加预付记录，公司的预付金额 = 原始预付金额+充值金额
                    company.setPrepayAmount(companyPrepayAmount + settlementPrepayAmount);
                }
                else {//如果是修改预付记录 ， 公司的预付金额 = 原始预付金额 - 最后一次充值金额 + 要更新的充值金额
                    oldBill = settlementPrepayRepository.findOne(settlementPrepayId);
                    company.setPrepayAmount(companyPrepayAmount-oldBill.getPrepayAmount()+settlementPrepayAmount);
                }
            }
            settlementCompanyRepository.save(company);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

        //保存预付记录
        settlementPrepayRepository.save(settlementPrepay);

    }

    @Override
    public Double getAllPrepayIncome(BusinessPrepayRequest $) {
        return settlementPrepayRepository.getAllPrepayIncome($);
    }

}
